package com.chat.datea.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chat.datea.R;
import com.chat.datea.bean.MusicList;
import com.chat.datea.bean.UserRecSinger;
import com.chat.datea.ui.MainActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xyb on 15/10/22.
 */
public class GuessSingerFragment_RecyclerView_Adapter extends RecyclerView.Adapter<GuessSingerFragment_RecyclerView_Adapter.MyViewHolder> {
    private Context context;
    private List<UserRecSinger.SonglistEntity> songlist;

    public GuessSingerFragment_RecyclerView_Adapter(Context context, List<UserRecSinger.SonglistEntity> songlist) {
        this.context = context;
        this.songlist = songlist;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.singer_viewpager_listview_item, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        if (songlist.get(position).getHas_mv_mobile() == 0) {
            holder.mv.setVisibility(View.GONE);
        } else {
            holder.mv.setVisibility(View.VISIBLE);
        }

        holder.music_name.setText(songlist.get(position).getTitle());
        holder.music_reson.setText(songlist.get(position).getAuthor());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<MusicList> lists = new ArrayList<>();
                Toast.makeText(context, "请稍候!音乐正在路上", Toast.LENGTH_SHORT).show();
                for (UserRecSinger.SonglistEntity songlistEntity : songlist) {
                    MusicList musicList = new MusicList();
                    musicList.setName(songlistEntity.getTitle());
                    musicList.setSinger(songlistEntity.getAuthor());
                    musicList.setSongId(songlistEntity.getSong_id());
                    musicList.setFrom("网络");
                    lists.add(musicList);
                }
                MainActivity.play(lists, position);
            }
        });


    }

    @Override
    public int getItemCount() {
        return songlist.size();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView music_name, music_reson;
        ImageView sq, mv;

        public MyViewHolder(View itemView) {
            super(itemView);
            music_name = (TextView) itemView.findViewById(R.id.music_name);
            music_reson = (TextView) itemView.findViewById(R.id.music_reson);
            sq = (ImageView) itemView.findViewById(R.id.sq);
            mv = (ImageView) itemView.findViewById(R.id.mv);

        }
    }
}
